package com.yx.camera_three;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Range;
import android.view.TextureView;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
    String TAG ="MainActivity";
    CameraManager cameraManager;//相机管理者
    CameraUtils cameraUtils1;
    CameraUtils cameraUtils2;
    CameraUtils cameraUtils3;
    ToggleButton open1;
    ToggleButton open2;
    ToggleButton open3;
    TextureView t1;
    TextureView t2;
    TextureView t3;
    private HandlerThread mBackgroundforcamera0Thread=null;
    private Handler mBackgroundforcamera0Handler;
    private HandlerThread mBackgroundforcamera2Thread=null;

    private Handler mBackgroundforcamera2Handler;
    private HandlerThread mBackgroundforcamera1Thread=null;
    private Handler mBackgroundforcamera1Handler;
    private static Range<Integer>[] fpsRanges0;
    private static Range<Integer>[] fpsRanges1;
    private static Range<Integer>[] fpsRanges2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cameraManager =(CameraManager) getSystemService(Context.CAMERA_SERVICE);

        init();
    }
    void init()
    {
        try {
            fpsRanges0=cameraManager.getCameraCharacteristics("0").get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
            fpsRanges1=cameraManager.getCameraCharacteristics("1").get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
            fpsRanges2=cameraManager.getCameraCharacteristics("2").get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
        } catch (CameraAccessException e) {
            throw new RuntimeException(e);
        }

        mBackgroundforcamera0Thread=new HandlerThread("0");
        mBackgroundforcamera0Thread.start();
        mBackgroundforcamera0Handler = new Handler(mBackgroundforcamera0Thread.getLooper());


        mBackgroundforcamera1Thread=new HandlerThread("1");
        mBackgroundforcamera1Thread.start();
        mBackgroundforcamera1Handler = new Handler(mBackgroundforcamera1Thread.getLooper());

        mBackgroundforcamera2Thread=new HandlerThread("2");
        mBackgroundforcamera2Thread.start();
        mBackgroundforcamera2Handler = new Handler(mBackgroundforcamera2Thread.getLooper());


        t1=findViewById(R.id.textureView);
        t2=findViewById(R.id.textureView1);
        t3=findViewById(R.id.textureView2);
        open1=findViewById(R.id.open1);
        open2=findViewById(R.id.open2);
        open3=findViewById(R.id.open3);
        open1.setOnCheckedChangeListener(this);
        open2.setOnCheckedChangeListener(this);
        open3.setOnCheckedChangeListener(this);
        cameraUtils1=new CameraUtils(cameraManager,"0",MainActivity.this,mBackgroundforcamera0Thread,mBackgroundforcamera0Handler,t1,fpsRanges0);
        cameraUtils2=new CameraUtils(cameraManager,"1",MainActivity.this,mBackgroundforcamera1Thread,mBackgroundforcamera1Handler,t2,fpsRanges1);
        cameraUtils3=new CameraUtils(cameraManager,"2",MainActivity.this,mBackgroundforcamera2Thread,mBackgroundforcamera2Handler,t3,fpsRanges2);
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId()== R.id.open1){
            if (isChecked){
                cameraUtils1.startPreview();
            }else {
                cameraUtils1.stopPreview();
            }
        }if (buttonView.getId()== R.id.open2){
            if (isChecked){
                cameraUtils2.startPreview();
            }else {
                cameraUtils2.stopPreview();
            }
        }if (buttonView.getId()== R.id.open3){
            if (isChecked){
                cameraUtils3.startPreview();
            }else {
                cameraUtils3.stopPreview();
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (cameraUtils1!=null){
            cameraUtils1.stopPreview();
            cameraUtils1.cameraDevice.close();
            cameraUtils1=null;
        }if (cameraUtils2!=null){
            cameraUtils2.stopPreview();
            cameraUtils2.cameraDevice.close();
            cameraUtils2=null;
        }if (cameraUtils3!=null){
            cameraUtils3.stopPreview();
            cameraUtils3.cameraDevice.close();
            cameraUtils3=null;
        }
        super.onDestroy();
    }
}